package com.rp.infopub.dao;

import org.raptor.common.DbOperUtil;
import org.raptor.orm.hibernate.DbaHibernateTemplateSupport;
import org.raptor.pagination.Page2;
import org.springframework.stereotype.Repository;

import com.rp.common.vo.PageParam;
import com.rp.infopub.po.Article;

@Repository
public class ArticleDao extends DbaHibernateTemplateSupport<Article> {

	public static final String INSERT_ARTICLE = 
			"INSERT INTO RP_INFOPUB_ARTICLES(" +
			"id,createDate,updateDate,version," +
			"channelId,content,summary,title," +
			"creatorId,creatorName,ip,channelName,clickNum,enabled2) " +
			"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	
	public static final String GET_ARTICLES_BY_CHANNELID = "from Article where channelId = ?";
	
	/**
	 * 分页获取文章数据
	 * @param pageParam
	 * @param title
	 * @param channelId
	 * @return
	 */
	public Page2 getArticlesByPage(PageParam pageParam, String title, String channelId){
		String hql = "";
		if(channelId == null || channelId.equals("")){
			hql = "from Article where title like ?";
			return this.getPage2ByOffset(hql, 
					pageParam.getPageIndex(), pageParam.getPageSize(), 
					pageParam.getSortField(), pageParam.getSortOrder(), 
					DbOperUtil.fuzzyQueryParam(title));
		}else{
			hql = "from Article where title like ? and channelId = ?";
			return this.getPage2ByOffset(hql, 
					pageParam.getPageIndex(), pageParam.getPageSize(), 
					pageParam.getSortField(), pageParam.getSortOrder(), 
					DbOperUtil.fuzzyQueryParam(title), channelId);
		}
	}
	
}
